<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\wlgzh\controller;

use cmf\controller\HomeBaseController;
use think\Validate;
use app\user\model\UserModel;
/**
 * Description of LoginController
 *
 * @author PVer
 */
class UserinfoController extends HomeBaseController {

    //put your code here
    public function dly() {
        return $this->fetch();
    }

    public function loginPost() {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'username' => 'require',
                'password' => 'require|min:6|max:32',
            ]);
            $validate->message([
                'username.require' => '用户名不能为空',
                'password.require' => '密码不能为空',
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $userModel = new UserModel();
            $user['user_pass'] = $data['password'];
            $user['user_login'] = $data['username'];
            $log = $userModel->doName($user);
            $session_login_http_referer = session('login_http_referer');
            $redirect = empty($session_login_http_referer) ? $this->request->root() : $session_login_http_referer;
            switch ($log) {
                case 0:
                    cmf_user_action('login');
                    $this->redirect('wlgzh/user/index');
//return $this->fetch(':index');
                    break;
                case 1:
                    $this->error(lang('PASSWORD_NOT_RIGHT'));
                    return $this->fetch(':dly');
                    break;
                case 2:
                    $this->error('账户不存在');
                    return $this->fetch(':dly');
                    break;
                case 3:
                    $this->error('账号被禁止访问系统');
                    return $this->fetch(':dly');
                    break;
                default :
                    $this->error('未受理的请求');
                    return $this->fetch(':dly');
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':dly');
        }
    }

    //获取验证码
    public function getYzm() {
        $data = $this->request->post();
        $phone = $data['phone'];
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $new_code = rand(0001, 9999);
        $codesModel = new CodesModel();
        $arr = array();
        $arr['code'] = $new_code;
        $arr['phone'] = $phone;
        $codesModel->save($arr);
        $smsConf = array(
            'key' => 'e494138099a285fb98318baeed2a60a5', //您申请的APPKEY
            'mobile' => $phone, //接受短信的用户手机号码
            'tpl_id' => '30395', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => "#code#=$new_code&#company#=聚真宝" //您设置的模板变量，根据实际情况修改
        );

        $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信

        if ($content) {
            $result = json_decode($content, true);
            $error_code = $result['error_code'];
            if ($error_code == 0) {
                //状态为0，说明短信发送成功
                echo "短信发送成功,短信ID：" . $result['result']['sid'];
            } else {
                //状态非0，说明失败
                $msg = $result['reason'];
                echo "短信发送失败(" . $error_code . ")：" . $msg;
            }
        } else {
            //返回内容异常，以下可根据业务逻辑自行修改
            echo "请求发送短信失败";
        }
    }

}
